package org.check.UI;

import java.awt.EventQueue;

import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.ListModel;
import javax.swing.AbstractListModel;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.awt.event.ActionEvent;
import javax.swing.JList;
import java.awt.Color;
import java.awt.Dialog.ModalityType;

import javax.swing.border.LineBorder;

import org.check.Util.XmlParser;
import org.check.VO.DBConnVO;

import javax.swing.JMenuBar;
import java.awt.Choice;
import javax.swing.JComboBox;
import javax.swing.JDialog;

import java.awt.Button;
import javax.swing.JLabel;
import com.jgoodies.forms.factories.DefaultComponentFactory;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EtchedBorder;

public class ConfigManager{

	/**
	 * 
	 */
	private static final long serialVersionUID = 7157668411787580013L;
	private JDialog dialog;
	private List<DBConnVO> dbConnVOs;
	private final String conns_config="conns_config.xml";
	private JList list;

	public JDialog getDialog() {
		return dialog;
	}

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ConfigManager window = new ConfigManager();
					window.dialog.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public ConfigManager(List<DBConnVO> dbConnVOs) {
		try {
			this.dbConnVOs=dbConnVOs;
			initialize();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public ConfigManager() {
		try {
			initialize();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Initialize the contents of the dialog.
	 */
	private void initialize() {
		dialog = new JDialog();
		dialog.setTitle("管理配置");
		dialog.setBounds(100, 100, 492, 280);
		dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		dialog.setResizable(false);
		dialog.setModalityType(ModalityType.APPLICATION_MODAL);
		dialog.getContentPane().setLayout(null);
		
		JButton btnNewButton_2 = new JButton("退出");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dialog.dispose();
			}
		});
		btnNewButton_2.setBounds(367, 208, 93, 23);
		dialog.getContentPane().add(btnNewButton_2);
		
		JButton btnNewButton_1 = new JButton("新建配置");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ConfigEditor newConfig = new ConfigEditor(dbConnVOs,dbConnVOs.size());
				newConfig.setVisible(true);
				reloadConfig();
			}
		});
		btnNewButton_1.setBounds(367, 47, 93, 23);
		dialog.getContentPane().add(btnNewButton_1);
		
		JLabel label = new JLabel("选择数据库配置");
		label.setBounds(27, 15, 93, 23);
		dialog.getContentPane().add(label);
		
		JButton button = new JButton("编辑配置");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String configName = (String) list.getSelectedValue();
				if (null != configName && !"".equals(configName)) {
					int selectedIndex = list.getSelectedIndex();
					ConfigEditor newConfig = new ConfigEditor(dbConnVOs,selectedIndex);
					newConfig.setVisible(true);
					reloadConfig();
				}else {
					JOptionPane.showMessageDialog(null, "请选择要编辑的配置。", "提示", JOptionPane.PLAIN_MESSAGE);
				}
			}
		});
		button.setBounds(367, 113, 93, 23);
		dialog.getContentPane().add(button);
		
		list = new JList(XmlParser.getNames(dbConnVOs));
		list.setBorder(new EtchedBorder(EtchedBorder.LOWERED, new Color(153, 204, 255), new Color(204, 204, 153)));
		list.setBounds(27, 48, 330, 183);
		dialog.getContentPane().add(list);
		
		JButton button_1 = new JButton("删除配置");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				if (JOptionPane.OK_OPTION==JOptionPane.showConfirmDialog(null, "无法撤销，确认删除？", "警告", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE)) {
					dbConnVOs.remove(list.getSelectedIndex());
					XmlParser.updateFromDBVOList(conns_config, dbConnVOs);
					reloadConfig();
				}
			}
		});
		button_1.setBounds(367, 146, 93, 23);
		dialog.getContentPane().add(button_1);
		
		JButton button_4 = new JButton("导入配置");
		button_4.setBounds(367, 80, 93, 23);
		dialog.getContentPane().add(button_4);
	}
	void reloadConfig(){
		dbConnVOs=XmlParser.generateDBVOList(conns_config);
		dialog.getContentPane().remove(list);
		list = new JList(XmlParser.getNames(dbConnVOs));
		list.setBorder(new EtchedBorder(EtchedBorder.LOWERED, new Color(153, 204, 255), new Color(204, 204, 153)));
		list.setBounds(27, 48, 330, 153);
		dialog.getContentPane().add(list);
		dialog.repaint();
	}
}
